/*
 * Copyright 2009 Douglas Lockamy, dlockamy_at_gmail.com
 * All rights reserved. Distributed under the terms of the MIT License.
 */


window_vfsNode = vfsList[vfsList.length]=new vfsNode();
window_vfsNode.Name = 'wmfs';

function wmNode() {
    fileNode.Name = "";
    fileNode.meta = 0;
    fileNode.next = 0;
    fileNode.prev = 0;
    fileNode.Data = 0;
    fileNode.Parent = 0;
    fileNode.parentClass = 0;
};

var winfs;

if (!this.wmfs) wmfs = {};

window_vfsNode.item=wmfs;

wmfs.version = '0.1';

wmfs.update = function(event)
{
    alert('!');
    
};

wmfs.init = function()
{
    winfs = new wmNode();
    winfs.Name="/";
    winfs.Type=1;
    winfs.prev=0;
    winfs.next=0;
    
    winfs.Data = new Array();
    winfs.Data[0] = new wmNode();
    
    var ctl = winfs.Data[0];
    ctl.Name="ctl";
    ctl.Type=2;
    ctl.prev=0;
    ctl.next=0;
    ctl.Data="a bunch of text";

    benix.registerMouseDown(this);
    benix.registerMouseUp(this);
    benix.registerMouseMove(this);
};

wmfs.mount =  function(serverAddress,fs_type,mountPoint,user,pass,argv)
{
    wmfs.init();
    return 0;
};
    
wmfs.umount = function()
{
    winfs = 0;
    return 0;
};

wmfs.read = function(path,flags,buffer,offset,length,sessionKey)
{
    if(flags =='1')
    {
        var readNode = wmfs.walk(path);

        if(readNode.Type == 1)
            return 1;
        if(readNode.Type==5)
        {
            return 1;
        }           
        else
            return 0;    
    }
    
    if(flags == '2')
    {
       var readNode = wmfs.walk(path);

        if(readNode.Type == 1)
            return readNode;
        if(readNode.Type==5)
        {
            return(readNode.Data[3].read(readNode.Name,flags,buffer,offset,length,sessionKey) );
        }           
        else
            return readNode;       
    }
    if(flags =='3')
    {  
        var readNode = wmfs.walk(path);
        benix.stdout(readNode.Type + '<br>');
        
        if(readNode.Type == 1)
        {
            if(readNode.Data[0].Name)
            return readNode.Data[0].Name;
            else
            return "File Read Error"
        }
        if(readNode.Type==5)
        {
            alert('recursive mounts not support yet');
            return -1;//readNode.Data[3].read(readNode.Name,flags) );
        }           
        else
            return readNode.Data;    
    }
    else 
        return -1;
};

wmfs.write = function(path,flags,buffer,offset,length,sessionKey)
{
/*
    FS File Read Flags
    1 Request Type
    2 Return Array of File in Directory
    3 Read text/data file*/
    var workPath = '/';
    var aNode = 0;
    var pathArray = path.split('/');
    var counter=0;
      
    while(counter <= pathArray.length-2)
    {
        if(workPath == '/')
            workPath = workPath + pathArray[counter];
        else
            workPath = workPath + '/' + pathArray[counter];
        counter++;
    }

    if(flags==0)
    {
        //unlink a node
            counter = 0;
            aNode = wmfs.walk(path);
            
            wmfs.removeWindow(aNode.Name);
            
            var prevNode = aNode.prev;   
            var nextNode = aNode.next;      

            while(aNode.Parent.Data[counter].Name != aNode.Name && counter < aNode.Parent.Data.length)
            {
                counter++;
            }
            
            aNode.Parent.Data.splice(counter,1);
            
            prevNode.next = nextNode;
            nextNode.prev = prevNode;
    }
    
    if(flags==1)
    {
        aNode = wmfs.walk(workPath);

        if(aNode.Type == 1 )
        {

            aNode.Data[aNode.Data.length] = new wmNode;//fileNode;
            var newDir = aNode.Data[aNode.Data.length-1];
            newDir.Name = pathArray[pathArray.length-1];
            newDir.Type = 1;
            newDir.next = 0;
            newDir.Data = new Array();
            newDir.prev = aNode.Data[aNode.Data.length-1];
            newDir.Parent = aNode;

            if(aNode.Data.length >= 2 )
            {        
                aNode.Data[aNode.Data.length-2].next=newDir;
            }  

            if(offset == "sync")
            {
                //alert("sync interface kit call");
                //BView and BWindow use this to create a file node when creating DOM directly
                //newDir.parentClass = findClassByDomId(pathArray[pathArray.length-1]);
                return 0;
            }

            else if(aNode.Name == "/")
            {                
                if(buffer)
                {               
                    newDir.parentClass =  new BDirectWindow(buffer,pathArray[pathArray.length-1],offset,length); 
                    return(newDir.parentClass );
                }
                else
                {
                    trueWindowFrame = new BFrame(10,10,500,600);
                    newDir.parentClass = new BDirectWindow(trueWindowFrame,pathArray[pathArray.length-1],offset,length);
                    return( newDir.parentClass  );
                }
            }
            else
            {
                benix.stdout(flags+" else "+pathArray[pathArray.length-1]);
                //newDir.parentClass = new BDirectView(buffer,pathArray[pathArray.length-1],offset,length);
                //aNode.parentClass.addChild(newDir.parentClass);
                return( 0);//newDir.parentClass );
            }
            
        }
        
        else
            return -1;
    }
    else if(flags==2)
    {
        aNode = wmfs.walk(workPath);

        if(aNode.Type==1 )
        {
            var newfile = aNode.Data[aNode.Data.length] = new wmNode;//fileNode();
            newfile.Name = pathArray[pathArray.length-1];
            newfile.Type = 2;
            newfile.next = 0;
            newfile.Data = buffer;
            newfile.prev = aNode.Data[aNode.Data.length-1];
            
            if(aNode.Data.length >= 2)
                aNode.Data[aNode.Data.length-2].next=newfile;  
            
            wmfs.addWindowStyle(aNode.Name,newfile.Name,buffer);
        }
        
        if(aNode.Type==5 )
        {
            var loadPath = benix.fsBuildMountPath(aNode,path);

            return(aNode.Data[3].write(loadPath,flags,buffer,offset,length,sessionKey) );
        }  
        
        else
            return -1;
    }
    
    else if(flags==3)
    {
  alert("wmfs write: "+wmfs.walk(workPath) );  
    }
    else 
        return -1;
};

wmfs.walk = function(fsNode)
{
 return(benix.fsWalk(winfs,fsNode) );
};

wmfs.isDrag = false;
wmfs.targetId = 0;
wmfs.target = 0;
wmfs.maxX = 700;
wmfs.maxY = 700;
wmfs.downPointX = 0;
wmfs.downPointY = 0;

wmfs.keyDown = function(event)
{
    //wmfs.target.KeyDown(event);
    //alert(event+" to "+wmfs.target.id);
};

wmfs.keyUp = function(event)
{
    wmfs.target.KeyUp(event);
};

wmfs.mouseDown = function(event)
{
    if(event.target.className == 'window')
        event.preventDefault();
    
    wmfs.downPointX = event.clientX;
    wmfs.downPointY = event.clientY;

    wmfs.isDrag = true;
    
    if(wmfs.target && wmfs.target != event.target.id && wmfs.target.style.zIndex > 0)
        wmfs.target.style.zIndex='1'; 
    
    if(event.target.nodeType == 3)
        event.target.parentNode.id;
    else 
    {
        if(event.target.className == 'window' )
        {
            wmfs.target = document.getElementById(event.target.id); 
             
            var indexFix = document.getElementById(event.target.id);
            
            if(indexFix)
                indexFix.style.zIndex='2';
        }      
        else
        {
            wmfs.target = document.getElementById(event.target.parentNode.id);
            
            var indexFix = document.getElementById(event.target.parentNode.id);
            if(indexFix)
                indexFix.style.zIndex='2';   
        }
            
        if(wmfs.target)
        {
            wmfs.target.left = wmfs.target.style.left;  
            wmfs.target.top = wmfs.target.style.top; 
        }        
    }
};

wmfs.mouseUp = function(event)
{
    event.preventDefault();
    wmfs.isDrag=false;
    
    wmfs.targetId = 0;
}

wmfs.mouseMove = function(event)
{
    event.preventDefault();
    if(!wmfs.target)
        return -1;

    if(wmfs.isDrag && wmfs.target.className == 'window' || wmfs.target.className == 'titleBar')
    {
        var x = parseInt( wmfs.target.left + 0 );
        var y = parseInt( wmfs.target.top + 0 );
        
        changex = event.clientX - wmfs.downPointX + x;
        changey = event.clientY - wmfs.downPointY + y; 
        if(changex <= wmfs.maxX && changey <= wmfs.maxY )
        {
            wmfs.target.style.left = changex;
            wmfs.target.style.top = changey;//e.clientY + y;//changey; 
        }  
    }
};

wmfs.addWindowStyle = function(divId,styleName,value)
{
    if(styleName == 'HTML')
        document.getElementById(divId).innerHTML = value;
    else if(document.getElementById(divId))
        document.getElementById(divId).setAttribute(styleName,value);
    else
        return -1;
};

wmfs.addDiv = function(windowName,divName)
{
   var newView = document.createElement('div');
    newView.id=divName;
    
    var parentView = "wView_"+windowName;
    if(document.getElementById(windowName))
        document.getElementById(parentView).appendChild(newView);
    else
        return 'Bad Window Name';
};

wmfs.removeWindow = function(windowName)
{
    var rmWin = document.getElementById(windowName);
    rmWin.parentNode.removeChild(rmWin);
}

